﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
using ARR.Entities;
using Telerik.Windows.Controls.GridView;
using ARR.UI.ViewModels;
using System.ServiceModel;
using ARR.UI.EnvoyImporterService;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for ImporterView.xaml
    /// </summary>
    public partial class SalesImporterView : UserControl
    {
        public SalesImporterView()
        {
            InitializeComponent();
        }

        private void ResultsGV_RowLoaded(object sender, RowLoadedEventArgs e)
        {
            if (e.Row is GridViewRow && !(e.Row is GridViewNewRow))
            {
                ImporterResult importResult = e.DataElement as ImporterResult;

                foreach (var cell in e.Row.Cells)
                {
                    if (cell.Column.Name == "SelectionColumn")
                    {
                        CheckBox check = cell.Content as CheckBox;
                        if (check != null)
                            check.Visibility = (importResult.Workaround.HasValue && importResult.Workaround == Workaround.CategoryMargin) ? Visibility.Visible : Visibility.Collapsed;
                    }

                    if (cell.Column.Name == "ButtonsColumn")
                    {
                        RadButton btnCategoryMatch = cell.Content as RadButton;
                        if (btnCategoryMatch != null)
                            btnCategoryMatch.Visibility = (importResult.Workaround.HasValue && importResult.Workaround == Workaround.MatchCategory) ? Visibility.Visible : Visibility.Collapsed;
                    }
                }
            }
        }

        private void ResultsGV_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            SalesImporterViewModel vm = this.DataContext as SalesImporterViewModel;

            if (vm != null)
            {
                ImporterResult result = ResultsGV.SelectedItem as ImporterResult;

                if (result != null)
                {
                    List<TempArticleSale> selectedSales = new List<TempArticleSale>();

                    if (result.ItemId != 0)
                    {
                        selectedSales = vm.model.TempArticleSalesCache.Where(x => x.Id == result.ItemId).ToList();
                    }
                    else if (result.Workaround == Workaround.Duplicated)
                    {
                        selectedSales = vm.model.TempArticleSalesCache.Where(x => x.ArticleName == result.Param2 &&
                                                                                  x.StoreCode == result.Param1 &&
                                                                                  x.Date.ToString("yyyyMM") == result.Param3).ToList();

                    }

                    if (selectedSales != null)
                    {
                        vm.model.TempArticleSales = new ObservableCollection<TempArticleSale>(selectedSales);

                        vm.TriggerPropertyChanged();
                    }
                    
                }



                int tempId = result.ItemId;

                if (tempId > 0)
                {
                    TempArticleSale sale = vm.TempArticleSales.SingleOrDefault(art => art.Id == tempId);

                    TempArticleSalesGV.ScrollIntoViewAsync(sale, //the row
                                         null, //the column
                                         new Action<FrameworkElement>((f) =>
                                         {
                                             (f as GridViewRow).IsSelected = true; // the callback method; if it is not necessary, you may set that parameter to null;
                                         }));
                }
            }
        }
    }
}
